<template>
	<!-- 添加车辆 -->
	<view class="addCar">
		<u-navbar title="添加配送工地" title-color="#333" back-icon-color="#333"></u-navbar>
		<view class="addCar-bottom">
			<view class="addCar-content-box">
				<!--  -->
				<view class="addCar-carName-phone">
					<text class="addCar-phone">配送工地：</text>
					<input class="addCar-input-one" type="text" placeholder="请输入配送工地" v-model="nameIpt" maxlength="20"/>
				</view>

				<view class="addCar-carName-phone">
					<text class="addCar-phone">运费单价：</text>
					<input class="addCar-input-one" type="number" @input="DPFreightAddUp" placeholder="请输入运费单价" v-model="phoneIpt"
					 maxlength="9" />
				</view>
				<view class="addCar-carName-phone">
					<text class="addCar-phone">联系人：</text>
					<input class="addCar-input-one" type="text" placeholder="请输入联系人" v-model="contactname"
					 maxlength="10" />
				</view>
				<view class="addCar-carName-phone">
					<text class="addCar-phone">联系电话：</text>
					<input class="addCar-input-one" type="number" placeholder="请输入联系电话" v-model="contactphone"
					 maxlength="11" />
				</view>
				<view class="addCar-carName-phone">
					<text class="addCar-phone">备注：</text>
					<input class="addCar-input-one" type="text" placeholder="请输入备注" v-model="note"
					 maxlength="40" />
				</view>
			</view>
			<!--  -->
			<view class="addCar-reset-determine">
				<view class="addCar-reset" @click="reset">重置</view>
				<view class="addCar-determine" @click="determine">确定</view>
			</view>
		</view>
		<u-modal v-model="addCarLoginShow" content="请确认是否添加该配送工地" :show-cancel-button="true" @confirm="addCarConfirm"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: JSON.parse(uni.getStorageSync('userinfo')),
				componeryArr: [],
				addressNew: null,
				Index: 0,
				diasabledInput: true, //可读不可写
				carNumberIpt: '', //车牌号
				nameIpt: '', //车主姓名
				phoneIpt: '', //联系方式
				contactname: '', 
				contactphone: '', 
				note: '', 
				addCarLoginShow: false,
				id:'',
				freightDP:'',
			}
		},
		onShow() {
			this.id=uni.getStorageSync('id')
			this.phoneIpt=uni.getStorageSync('freightDP')
		},
		methods: {

			//配送单价
			DPFreightAddUp() {
				var value = this.phoneIpt;
				var num = this.clearNoNum(value);
				if(num<=0) return
				if(num>100000){
					uni.showToast({
						title: '单价高于最大限制100000',
						duration: 5000,
						icon: 'none',
					});
				}
				num=num>100000?100000:num
				this.$nextTick(function(){
					this.phoneIpt=num
				})
			},
			clearNoNum(value) {
				value = value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
				value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
				value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
				value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
				if (value.indexOf(".") < 0 && value != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
					value = parseFloat(value);
				}
				return value;
			},
			// 确定
			determine() {
				if(this.nameIpt.trim()==''){
					uni.showToast({
						title: '配送地不可为空',
						duration: 5000,
						icon: 'none',
					});
					return 
				}
				if(this.phoneIpt===''){
					uni.showToast({
						title: '运费单价不可为空',
						duration: 5000,
						icon: 'none',
					});
					return
				}
				if(this.contactphone!=''){
					var reg = /^1[3-9][0-9]{9}$/;
					if(!reg.test(this.contactphone)){
						uni.showToast({
							title: '请输入有效手机号',
							duration: 5000,  
							icon: 'none',
						});
						return 
					}
				}
				this.addCarLoginShow = true
			},
			addCarConfirm() {
				uni.request({
					url: this.baseUrl + '/orm/ormtb405/insert',
					method: 'POST',
					dataType: 'json',
					data: {
						ORMTB404ID: this.id,
						sign: this.userinfo.sign,
						ORMTB405CA00: this.nameIpt,
						ORMTB405CA01: this.phoneIpt,
						ORMTB405CA02: this.contactname,
						ORMTB405CA03: this.contactphone,
						ORMTB405CA04: this.note,
					},
					success: (res) => {
						// console.log(res);
						if (res.data.code == 200) {
							uni.showToast({
								title: '添加成功',
								duration: 5000,
								icon: 'none',
							});
							uni.navigateBack()
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 5000,
								icon: 'none',
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: res.data.message,
							duration: 5000,
							icon: 'none',
						});
					}
				});
			},
			// 重置
			reset() {
				this.nameIpt = '' //车主姓名
				// this.phoneIpt = '' //联系方式
				this.phoneIpt=uni.getStorageSync('freightDP')
				this.contactname=""
				this.contactphone=""
				this.note=""
			},
		}
	}
</script>

<style scoped>
	.addCar input {
		border: 0;
	}

	.addCar {
		width: 100%;
		min-height: 100vh;
		background-color: rgba(242, 242, 242, 1);
		overflow: hidden;
	}

	/* 列表 */

	/* 内容 */
	.addCar-content-box {
		width: 100%;
		background: rgba(255, 255, 255, 1);
		padding: 0 24rpx;
	}

	.addCar-carName-phone {
		border-bottom: 2rpx solid rgba(230, 230, 230, 1);
		padding: 18rpx 0;
	}

	.addCar-input-one {
		width: 310rpx !important;
		display: inline-block;
		margin-bottom: 0 !important;
		padding: 0 !important;
		height: 44rpx !important;
		/* margin-left: 0.1rem; */
		color: rgba(51, 51, 51, 1);
		font-size: 28rpx;
		text-align: center;
	}

	.addCar-phone {
		width: 238rpx;
		display: inline-block;
		font-size: 30rpx;
		color: rgba(102, 102, 102, 1);
		/* background: #000000; */
	}

	.addCar-phone-placeholder {
		font-size: 30rpx;
		color: rgba(102, 102, 102, .5);
	}

	.addCar-dun {
		color: rgba(51, 51, 51, 1);
		font-size: 30rpx;
	}

	/* 按钮 */
	.addCar-reset-determine {
		margin-top: 167rpx;
		display: flex;
	}

	.addCar-reset-determine>view {
		width: 159rpx;
		text-align: center;
		line-height: 65rpx;
		color: rgba(255, 255, 255, 1);
		font-size: 32rpx;
	}

	.addCar-reset {
		width: 159rpx;
		background: rgba(234, 197, 15, 1);
		text-align: center;
		margin-left: 170rpx;
	}

	.addCar-determine {
		background: rgba(0, 161, 244, 1);
		margin-left: 60rpx;
	}

	.addCar-phone-car {
		display: inline-block;
		width: 310rpx;
		height: 44rpx;
		line-height: 44rpx;
		/* border: 1px solid rgba(0, 0, 0, .2); */
		/* border-radius: 3px; */
		text-align: center;
		vertical-align: middle;
		font-size: 30rpx;
	}

	input::placeholder {
		color: rgba(102, 102, 102, .5);
	}
</style>
